import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.Icon;

/**
 * Title:  Group.java
 * Description:  This class is used for adding a close icon to each tab
 * Copyright:    Copyright (c) 2009
 * @author Dai Jingyi?   annotated by WANG Jiechao
 * @version 1.0
 */

public class CloseTabIcon implements Icon  {   
    private int x_pos;   
    private int y_pos;   
    private int width;   
    private int height;   
    private Icon fileIcon; 
    
	/**
	 * this method is used to add file icon with size 16*16
	 */
    public CloseTabIcon(Icon fileIcon) {   
        this.fileIcon = fileIcon;   
        width = 16;   
        height = 16;   
    }   
	/**
	 * this method is used to draw the close icon
	 */
    public void paintIcon(Component c, Graphics g, int x, int y) {   
        this.x_pos = x;//define position variables for drawing   
        this.y_pos = y;   
        Color col = g.getColor();//define color for drawing   
        g.setColor(Color.black);   
        int y_p = y + 2;           
        //draw a square
        g.drawLine(x + 1, y_p, x + 12, y_p);   
        g.drawLine(x + 1, y_p + 13, x + 12, y_p + 13);   
        g.drawLine(x, y_p + 1, x, y_p + 12);   
        g.drawLine(x + 13, y_p + 1, x + 13, y_p + 12);  
        //draw a close mark
        g.drawLine(x + 3, y_p + 3, x + 10, y_p + 10);   
        g.drawLine(x + 3, y_p + 4, x + 9, y_p + 10);   
        g.drawLine(x + 4, y_p + 3, x + 10, y_p + 9);   
        g.drawLine(x + 10, y_p + 3, x + 3, y_p + 10);   
        g.drawLine(x + 10, y_p + 4, x + 4, y_p + 10);   
        g.drawLine(x + 9, y_p + 3, x + 3, y_p + 9);   
        g.setColor(col);//set the color   
        if (fileIcon != null) {//it seems to call this function again, but I cannot see any effect.
            fileIcon.paintIcon(c, g, x + width, y_p);   
        }   
    }  
	/**
	 * The following methods must be implemented in order to use CloseTabIcon
	 */   
    public int getIconWidth() {   
        return width + (fileIcon != null ? fileIcon.getIconWidth() : 0);   
    } 
    
    public int getIconHeight() {   
        return height;   
    } 
	/**
	 * This method returns a rectangle starting at x_pos, y_pos and has size of width*height
	 */    
    public Rectangle getBounds() {   
        return new Rectangle(x_pos, y_pos, width, height);   
    }   
}  


